﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SportsStore.Domain.Abstract;
using SportsStore.WebUI.Models;

namespace SportsStore.WebUI.Controllers
{
    public class ProductController : Controller
    {
        public int PageSize = 4;
        private readonly IProductsRepository _repository;

        public ProductController(IProductsRepository productsRepository)
        {
            _repository = productsRepository;
        }

        //
        // GET: /Product/

        public ViewResult List(string category, int page = 1)
        {
            // TODO: page index verification
            var viewModel =
                new ProductsListViewModel
                    {
                        Products =
                            _repository.Products
                                       .Where(p => category == null || p.Category == category)
                                       .OrderBy(p => p.ProductId)
                                       .Skip((page - 1)*PageSize)
                                       .Take(PageSize),
                        PagingInfo =
                            new PagingInfo()
                                {
                                    CurrentPage = page,
                                    ItemsPerPage = PageSize,
                                    TotalItems = 
                                    _repository.Products.Count(p => category == null || p.Category == category)
                                },
                        CurrentCategory = category
                    };

            return View(viewModel);
        }
    }
}
